#include "net.h"


//客户端
int main()
{

	int fd = -1;
	//创建套接字
	struct sockaddr_in soc;
    //创建套接字
	if( (fd = socket( AF_INET, SOCK_STREAM, 0)) < 0)
    {
        perror("socket");
        exit(1);
     }


	//
	/* 填充结构体 */
	 soc.sin_family = AF_INET;
     soc.sin_port = htons(PORT);
 #if 1
     /* ipv4 */
     soc.sin_addr.s_addr = inet_addr(IP_ADDR4);

 #else

     /* ipv4 ipv6 都可以用 */
     if( (inet_pton(AF_INET, IP_ADDR, (void*)soc.sin_addr)) < 0)
	 {
		perror("inet_pton");
        exit(0);
     }
#endif


	if( connect(fd, (struct sockaddr *)&soc, sizeof(soc)) < 0) {
		perror("connect");
		exit(1);
	}

	printf("Client staring...OK!\n");
	/*3. 读写数据 */
	char buf[BUFSI];
	int ret = -1;
	while(1) {
		bzero(buf, BUFSI);
		if( fgets(buf, BUFSIZ-1, stdin) ==NULL) {
			continue;
		}
		do {
			ret = write(fd, buf, strlen(buf));
		}while(ret < 0 && EINTR == errno);

		if( !strncasecmp(buf, QUIT, strlen(QUIT))) {  //用户输入了quit字符
                        printf("Client is exiting!\n");
                        break;
                }
	}

	/*4.关闭套接字 */
	close(fd);
}



















